<?php
function fetch($label, $max_length=false, $sanitize = true) {
	$get_str = false;
	$post_str = false;

	if (isset($_GET[$label])) {
		$get_str = ($max_length && strlen(trim($_GET[$label]))>$max_length)? substr(trim($_GET[$label]), 0, $max_length) : trim($_GET[$label]);
		$get_str = str_replace("/", "", $get_str);
	}
	if (isset($_POST[$label])) {
		$post_str = ($max_length && strlen(trim($_POST[$label]))>$max_length)? substr(trim($_POST[$label]), 0, $max_length) : trim($_POST[$label]);
	}
	$result_get = ($get_str && $sanitize)? sanitize($get_str) : $get_str;
	$result_post = ($post_str && $sanitize)? sanitize($post_str) : $post_str;
	$result = ($result_post)? $result_post : $result_get; //favor POSTED variable
	return $result;
}

function sanitize($string) {
	$string = trim($string);
	$string = str_replace("<", "&lt;", $string);
	$string = str_replace(">", "&gt;", $string);
	$string = strip_tags($string);
	return Sql::Sanitize($string);
}

function str_hash($str1, $str2) {
	$str1 = strrev(strtolower($str1));
	$str2 = strrev(strtolower($str2));
	$str1Array = str_split($str1);
	$str2Array = str_split($str2);
	if (count($str1Array)>count($str2Array)) {
		$set_1 = $str1;
		$set_2 = $str2;
	} else {
		$set_1 = $str2;
		$set_2 = $str1;
	}
	$count = 0;
	$hash = "";
	foreach($set_1 as $letter) {
		$hash .= $letter;
		if (isset($set_2[$count])) {
			$hash .= $set_2[$count];
			$count++;
		}
	}
	$hash = sha1($hash);
	return substr($hash, 0, 35);
}

function getDBConnection() {
	if (!$GLOBALS['DBConnection'] || !$GLOBALS['DBSelectDB']) {
		$dbHost = $GLOBALS['DBHost'];
		$dbUsername = $GLOBALS['DBUsername'];
		$dbPassword = $GLOBALS['DBPassword'];
		$dbDatabase = $GLOBALS['DBDatabase'];
		$GLOBALS['DBConnection'] = new mysqli($dbHost, $dbUsername, $dbPassword, $dbDatabase);
	}
	return $GLOBALS['DBConnection'];
}
?>